package com.wangan.wanganpush.stomp.sessionHandle;

import com.google.gson.Gson;
import com.wangan.wanganpush.dao.VehicleDao;
import com.wangan.wanganpush.model.Vehicle;
import com.wangan.wanganpush.stomp.RelinkMap.ThreadMap;
import com.wangan.wanganpush.stomp.relinkThread.LoginThread;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompFrameHandler;
import org.springframework.messaging.simp.stomp.StompHeaders;
import org.springframework.messaging.simp.stomp.StompSession;

import java.lang.reflect.Type;
import java.util.concurrent.CountDownLatch;

public class VehicleHandler extends  BaseStompSessionHandler{

    private static Logger log = LogManager.getLogger(VehicleHandler.class);

    VehicleDao vehicleDao;

    private String sub;
    private Object toSend;
    private String dest;
    CountDownLatch latch;
    private boolean asignedVehicle = false;

    boolean isConnected = false;
    boolean isConnected1 = false;

    public VehicleHandler(String sub, String dest, Object toSend, CountDownLatch latch
            , VehicleDao vehicleDao) {
        this.sub = sub;
        this.toSend = toSend;
        this.dest = dest;
        this.latch = latch;
        this.vehicleDao = vehicleDao;
    }

    @Override
    public void handleFrame(StompHeaders headers, Object payload) {
        super.handleFrame(headers, payload);
    }

    @Override
    public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
        super.afterConnected(session, connectedHeaders);
        ThreadMap.removeThreadName("Vehicle");
        System.err.println("连接成功！");
        isConnected = false;
        isConnected1 = false;
        session.subscribe(sub , new StompFrameHandler() {

            @Override
            public Type getPayloadType(StompHeaders headers) {
                System.out.println(headers.toString());

                return null;
            }

            //数据推送结果
            @Override
            public void handleFrame(StompHeaders headers, Object payload) {
                log.info("handle subscribe message.");
                Vehicle u = null;
                try {
                    System.out.println(payload);
                    Gson gs = new Gson();
                    u = gs.fromJson(new String((byte[])payload,"utf-8"), Vehicle.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                u.setFlag(0);

                System.out.println("存数据库："+u.toString());
                vehicleDao.saveAndFlush(u);
                System.out.println("handle subscribe message.");
            }
        });

    }

    @Override
    public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
        super.handleException(session, command, headers, payload, exception);
        log.error("stomp error: " + exception);
        log.error("stomp error: " + new String(payload));
        dataPro(isConnected1);
    }

    @Override
    public void handleTransportError(StompSession session, Throwable exception) {
        log.error("stomp transport error: {}", exception);
        dataPro(isConnected);
    }

    public boolean isAsignedVehicle() {
        return asignedVehicle;
    }

    private void dataPro(boolean flag){

        if(!flag){
            if(!ThreadMap.isExist("Vehicle")){
                LoginThread login = new LoginThread("Vehicle",this);
                login.start();
                //把登录数据的重连线程放进列表
                ThreadMap.addThread("Vehicle");
            }
            flag = true;
        }


    }





}
